using System;
using System.CodeDom;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace CodeDom
{
  internal class FieldWrapperPropertyGenerator : TemplateFieldGenerator
  {
    private readonly string _fieldWrapperClassName;

    public FieldWrapperPropertyGenerator(TemplateField field, string fieldWrapperClassName)
      : base(field)
    {
      _fieldWrapperClassName = fieldWrapperClassName;
    }

    #region Overrides of FieldPropertyGenerator

    public override void AddValueConversionStatements(CodeExpression itemReference, CodeMemberMethod method)
    {
      CodeExpression fieldIDExpression = new CodeObjectCreateExpression("ID", new CodePrimitiveExpression(this.TemplateField.ID.ToString()));
      var getFieldExpression = new CodeIndexerExpression(new CodePropertyReferenceExpression(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "InnerItem"), "Fields"), fieldIDExpression);
      var castExpression = new CodeCastExpression(_fieldWrapperClassName, getFieldExpression);
      var returnStatement = new CodeMethodReturnStatement(castExpression);
      method.Statements.Add(returnStatement);
    }

    public override CodeTypeReference GetFieldType()
    {
      return new CodeTypeReference(_fieldWrapperClassName);
    }

    #endregion
  }
}